﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace awaitTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // async修饰一个异步方法，方法内部必须有await，否则还是一个同步方法
        private async void button1_Click(object sender, EventArgs e)
        {
            var result = Task.Run(() =>
            {
                Thread.Sleep(10000);
                return "sleep 10 s";
            });

            // 同步方法，阻塞UI线程
            //this.label1.Text = result.Result;
            
            // 异步方法，不会阻塞UI线程，await修饰Task或者Task<T>
            this.label1.Text = await result;
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            var result = Task.Run(() =>
            {
                Thread.Sleep(5000);
                return "sleep 5 s";
            });

            this.label2.Text = await result;
        }
    }
}
